//
//  AttackCell.m
//  mendianjieke
//
//  Created by 响当当 on 7/31/19.
//  Copyright © 2019 dongzheng. All rights reserved.
//

#import "AttackCell.h"

@interface AttackCell()
@property (nonatomic, strong)UIImageView *iconImageView;
@property (nonatomic, strong)UILabel *indexLabel;//标题
@property (nonatomic, strong)UILabel *titleLabel;//标题
@property (nonatomic, strong)UILabel *subTitleLabel;//副标题
@property (nonatomic, strong)UIView *lineView;
@end

@implementation AttackCell

- (UIImageView *)iconImageView
{
    if (!_iconImageView)
    {
        _iconImageView = [[UIImageView alloc]initWithFrame:CGRectMake(20*kUIScaleBasePlus,25*kUIScaleBasePlus,27*kUIScaleBasePlus,27*kUIScaleBasePlus)];
        _iconImageView.image = [UIImage imageNamed:@"xing"];
    }
    return _iconImageView;
}

- (UILabel *)indexLabel
{
    if (!_indexLabel)
    {
        _indexLabel = [Utilties creatLabelWithFrame:CGRectMake(CGRectGetMaxX(self.iconImageView.frame)+11.5*kUIScaleBasePlus, 25*kUIScaleBasePlus,SCREEN_WIDTH-58.5*kUIScaleBasePlus,10.5*kUIScaleBasePlus)
        textAligment:NSTextAlignmentLeft
            textClor:LCHexColor(0x999999)
            textFont:11*kUIScaleBasePlus];
    }
    return _indexLabel;
}


- (UILabel *)titleLabel
{
    if (!_titleLabel)
    {
        _titleLabel = [Utilties creatLabelWithFrame:CGRectMake(CGRectGetMaxX(self.iconImageView.frame)+11.5*kUIScaleBasePlus, 43*kUIScaleBasePlus,SCREEN_WIDTH-58.5*kUIScaleBasePlus,14.5*kUIScaleBasePlus)
                                       textAligment:NSTextAlignmentLeft
                                           textClor:LCHexColor(0x212121)
                                           textFont:15*kUIScaleBasePlus];
    }
    return _titleLabel;
}

- (UILabel *)subTitleLabel
{
    if (!_subTitleLabel)
    {
        _subTitleLabel = [Utilties creatLabelWithFrame:CGRectMake(CGRectGetMaxX(self.iconImageView.frame)+11.5*kUIScaleBasePlus, CGRectGetMaxY(self.titleLabel.frame),300*kUIScaleBasePlus,20*kUIScaleBasePlus)
                                       textAligment:NSTextAlignmentLeft
                                           textClor:LCHexColor(0x212121)
                                           textFont:15*kUIScaleBasePlus];
        _subTitleLabel.numberOfLines= 0;
    }
    return _subTitleLabel;
}

- (UIView *)lineView
{
    if (!_lineView)
    {
        _lineView = [[UIView alloc]initWithFrame:CGRectZero];
        _lineView.backgroundColor = JSALineColor;
    }
    return _lineView;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        [self.contentView addSubview:self.iconImageView];
        [self.contentView addSubview:self.indexLabel];
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.subTitleLabel];
//        [self.contentView addSubview:self.lineView];
    }
    return self;
}

- (void)setModel:(AttackModel *)model
{
    _model = model;
    
    self.indexLabel.text = model.index;
    self.titleLabel.text = model.title;

    if (model.num.integerValue > 1)
    {
        [self.titleLabel setWidth:104*kUIScaleBasePlus];
        [self.titleLabel setHeight:27*kUIScaleBasePlus];
        self.titleLabel.font = [UIFont systemFontOfSize:12*kUIScaleBasePlus];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.layer.masksToBounds = YES;
        self.titleLabel.textColor = LCHexColor(0x02B98E);
        self.titleLabel.layer.cornerRadius = self.titleLabel.frame.size.height/2;
        self.titleLabel.layer.borderWidth = 1;
        self.titleLabel.layer.borderColor = LCHexColor(0x02B98E).CGColor;
    }
    
    if ([model.subtitle isEqualToString:@""])
    {
        self.lineView.frame = CGRectMake(0,CGRectGetMaxY(self.titleLabel.frame)+14*kUIScaleBasePlus,SCREEN_WIDTH,1*kUIScaleBasePlus);
    }
    else
    {
        self.subTitleLabel.text = model.subtitle;
        [self.subTitleLabel setY:CGRectGetMaxY(self.titleLabel.frame)+9.5*kUIScaleBasePlus];
        [self.subTitleLabel sizeToFit];
        self.lineView.frame = CGRectMake(0,CGRectGetMaxY(self.subTitleLabel.frame)+14*kUIScaleBasePlus,SCREEN_WIDTH,1*kUIScaleBasePlus);

    }
}


#pragma mark - 计算cell高度
/** 获取cell高度 */
+ (CGFloat)heightWithModel:(AttackModel *)model
{
    NSString *string = model.title;
    
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:string];
    
    NSRange allRange = [string rangeOfString:string];
    
    [attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14*kUIScaleBasePlus] range:allRange];
    
    [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor darkGrayColor]range:allRange];
    
    CGFloat titleHeight;
    
    NSStringDrawingOptions options =  NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
    
    // 获取label的最大宽度
    CGRect rect = [attrStr boundingRectWithSize:CGSizeMake(SCREEN_WIDTH, CGFLOAT_MAX)options:options context:nil];
    
    titleHeight = ceilf(rect.size.height);
    
    
    
    CGFloat subtitleHeight = 0;

    NSString *subTitle = model.subtitle;
    
    if (![subTitle isEqualToString:@""])
    {
        NSMutableAttributedString *subattrStr = [[NSMutableAttributedString alloc] initWithString:subTitle];
        
        NSRange suballRange = [subTitle rangeOfString:subTitle];
        
        [subattrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13*kUIScaleBasePlus] range:suballRange];
        
        [subattrStr addAttribute:NSForegroundColorAttributeName value:[UIColor darkGrayColor]range:suballRange];
        
        NSStringDrawingOptions suboptions =  NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
        
        // 获取label的最大宽度
        CGRect subrect = [subattrStr boundingRectWithSize:CGSizeMake(SCREEN_WIDTH, CGFLOAT_MAX)options:suboptions context:nil];
        
        subtitleHeight = ceilf(subrect.size.height);
    }
    
    return titleHeight + 60*kUIScaleBasePlus + subtitleHeight; // 动态高度 + 静态高度
}
@end
